package com.usefullc.user.controller;

import com.usefullc.common.util.BeanUtils;
import com.usefullc.system.entity.Pager;
import com.usefullc.system.entity.WebResult;
import com.usefullc.system.controller.WebResultBuilder;
import com.usefullc.system.param.IdsParam;
import com.usefullc.user.domain.Position;
import com.usefullc.user.domain.Role;
import com.usefullc.user.param.RoleAuthResParam;
import com.usefullc.user.query.RoleQuery;
import com.usefullc.user.service.AuthResService;
import com.usefullc.user.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

import static com.usefullc.system.controller.WebResultBuilder.successResult;

@RestController
@Api( tags="角色管理")
@RequestMapping(path = "/user/role/")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private AuthResService authResService;


    @GetMapping(path = "list",produces = "application/json")
    @ApiOperation(value = "获取角色分页列表",response = WebResult.class)
    public WebResult list(RoleQuery query){
        Pager<Role> pager = roleService.findByPage(query);
        return successResult(pager);
    }

    @GetMapping(path = "listAll",produces = "application/json")
    @ApiOperation(value = "获取角色所有列表",response = WebResult.class)
    public WebResult listAll(RoleQuery query){
        List<Role> dataList = roleService.findAll(query);
        return successResult(dataList);
    }


    @ApiOperation(value = "保存角色", response = WebResult.class)
    @PostMapping(path = "save", produces = "application/json")
    public WebResult save(@Valid @RequestBody Role role) {
        roleService.save(role);
        return WebResultBuilder.successResult(role);
    }

    @ApiOperation(value = "修改角色", response = WebResult.class)
    @PostMapping(path = "update", produces = "application/json")
    public WebResult update(@Valid @RequestBody Role role) {
        Role oldBean = roleService.findById(role.getId());
        BeanUtils.beanCopy(role, oldBean);
        roleService.save(oldBean);
        return WebResultBuilder.successResult(role);
    }

    @ApiOperation(value = "保存角色资源", response = WebResult.class)
    @PostMapping(path = "saveRoleAuthRes", produces = "application/json")
    public WebResult saveRoleAuthRes(@Valid @RequestBody RoleAuthResParam param) {
        roleService.saveRoleAuthRes(param);
        return WebResultBuilder.successResult(param);
    }


    @ApiOperation(value = "删除角色", response = WebResult.class)
    @PostMapping(path = "del", produces = "application/json")
    public WebResult del(@RequestBody IdsParam idsParam) {
        roleService.deletePhysByIds(idsParam.getIds());
        return WebResultBuilder.successResult();
    }


}
